package cz.muni.fi.pa165.sme.common;

import javax.persistence.EntityManager;

import cz.muni.fi.pa165.sme.dao.api.PersistentEntity;

/**
 * @author forman
 */
public class EntityValidator {

	private static final String NULL_ENTITY_MSG = "Entity cannot be null";

    //-----------------------------------------------------------

	/**
	 * Validates new entity.
	 * @param entity to be validated
	 */
    public static void validateNewEntity(final PersistentEntity entity) {
        if(entity == null) {
            throw new IllegalArgumentException(NULL_ENTITY_MSG);
        }

        if(entity.getId() != null) {
            throw new IllegalArgumentException("New entity cannot have assigned id.");
        }
    }

	/**
	 * Validates entity for update.
	 * @param entity entity for update
	 */
    public static void validateUpdatedEntity(final PersistentEntity entity) {
        if(entity == null) {
            throw new IllegalArgumentException(NULL_ENTITY_MSG);
        }

        if(entity.getId() == null) {
            throw new IllegalArgumentException("Existing entity must have assigned ID.");
        }
    }

    public static void validateEntityManager(final EntityManager entityManager){
        if(entityManager == null){
            throw new IllegalStateException("Entity manager is not set.");
        }
    }

    public static void validateDeleteEntity(final PersistentEntity entity){
        if(entity.getId() == null) {
            throw new IllegalArgumentException("ID must not be null if object is to be deleted.");
        }
    }

    public static void validateGetEntity(final Long id){
        if(id == null) {
            throw new IllegalArgumentException("ID to be looked up cannot be null");
        }
    }
}
